package com.shuzi.MediaShare.gui.movie;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.base.BaseFrameAtivity;
import com.shuzi.MediaShare.control.ImageLoaderManager;
import com.shuzi.MediaShare.gui.DetailViewPagerAdapter;
import com.shuzi.MediaShare.model.MediaDownLoadLinkInfo;
import com.shuzi.MediaShare.model.MediaType;
import com.shuzi.MediaShare.model.MovieInfo;
import com.shuzi.MediaShare.net.Protocol;
import com.shuzi.MediaShare.view.TypeChoiceLayout;

/**
 * 电影详情页面
 * 
 * @author lung 上午1:36:37 2013
 */
public class MovieDetailActivity extends BaseFrameAtivity
{

	private MovieInfo movieInfo = null;

	/** 海报图片 */
	private ImageView movieImg = null;

	/** 名称 */
	private TextView movieName = null;

	/** 發佈時間 */
	private TextView movieReleaseTime = null;

	/** 主演 */
	private TextView movieScreenwriter = null;

	/** 导演 */
	private TextView movieDirector = null;

	/** 地区 */
	private TextView movieArea = null;

	/** 类型 */
	private TextView movieType = null;

	/** 评分 */
	private TextView movie_grade = null;

	/** 图片加载 */
	private ImageLoaderManager imageLoaderManager;

	private ViewPager viewPager;

	/** 电影下载地址集数 */
	private List<MediaDownLoadLinkInfo> mediaDownLoadLinkInfos;

	private DetailViewPagerAdapter adapter;

	private ImageView playMoViewButton = null;

	private ImageView downloadMovieButton = null;

	private TypeChoiceLayout typeChoiceLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		loadData();
	}

	@Override
	protected boolean initData(Integer... params)
	{
		boolean isSuccess = true;
		mediaDownLoadLinkInfos = Protocol.queryVideo(movieInfo.getType(),
				movieInfo.getID());
		if(mediaDownLoadLinkInfos!=null&&mediaDownLoadLinkInfos.size()==0){
			isSuccess = false;
		}
		return isSuccess;
	} 

	@Override
	protected void refreshView(boolean isSuccessful, Integer... params)
	{
		if (isSuccessful)
		{
			typeChoiceLayout.setInfo(this,
					Constant.tMap.get(movieInfo.getType()));
			adapter = new DetailViewPagerAdapter(getDetailViewList());
			viewPager.setAdapter(adapter);
		} else
		{
			Toast.makeText(this, "暂无详情信息", Toast.LENGTH_SHORT).show();
			finish();
		}

	}

	@Override
	protected void tryAgain()
	{

	}

	@Override
	protected View setCenterView()
	{
		movieInfo = (MovieInfo) getIntent().getSerializableExtra("MOVIEINFO");
		imageLoaderManager = ImageLoaderManager.getInstace();
		View view = View.inflate(this, R.layout.movie_detail, null);

		movieImg = (ImageView) view.findViewById(R.id.movie_detail_img);
		movieName = (TextView) view.findViewById(R.id.movie_detail_name);
		movieReleaseTime = (TextView) view
				.findViewById(R.id.movie_detail_release_time);
		movieScreenwriter = (TextView) view
				.findViewById(R.id.movie_detail_screenwriter);
		movieDirector = (TextView) view
				.findViewById(R.id.movie_detail_director);
		movieArea = (TextView) view.findViewById(R.id.movie_detail_area);
		movieType = (TextView) view.findViewById(R.id.movie_detail_type);
		viewPager = (ViewPager) view.findViewById(R.id.movie_viewpager);
		movie_grade = (TextView) view.findViewById(R.id.movie_detail_grade);
		playMoViewButton = (ImageView) view
				.findViewById(R.id.movie_detail_start);
		downloadMovieButton = (ImageView) view
				.findViewById(R.id.movie_detail_download);
		typeChoiceLayout = (TypeChoiceLayout) view
				.findViewById(R.id.movie_choice_layout);

		imageLoaderManager.displayImage(movieInfo.getPoster_link(), movieImg,
				imageLoaderManager.options);
		movieName.setText(movieInfo.getName_cn());
		movieReleaseTime.setText(String.format(
				getResources().getString(R.string.release_time),
				movieInfo.getRelease_time()));
		movieScreenwriter.setText(String.format(
				getResources().getString(R.string.screenwriter),
				movieInfo.getScreenwriter()));
		movieDirector.setText(String.format(
				getResources().getString(R.string.director),
				movieInfo.getDirector()));
		movieArea.setText(String.format(
				getResources().getString(R.string.area),
				movieInfo.getProd_area()));
		MediaType type = Constant.tMap.get(movieInfo.getType());
		// movieType.setText(String.format(getResources()
		// .getString(R.string.other), type.typeList[Integer
		// .parseInt(movieInfo.getGenre())]));
		movie_grade.setText(movieInfo.getGrade());

		playMoViewButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (mediaDownLoadLinkInfos != null
						&& mediaDownLoadLinkInfos.size() != 0)
				{
					Intent intent = new Intent();
					intent.setClass(MovieDetailActivity.this,
							MovieDetailPlayerActivity.class);
					intent.putExtra("URL", Protocol.BASEPATH
							+ mediaDownLoadLinkInfos.get(0).getFilepath());
					startActivity(intent);
				} else
				{
					Toast.makeText(MovieDetailActivity.this, "播放地址为空",
							Toast.LENGTH_SHORT).show();
					// Intent intent = new Intent();
					// intent.setClass(MovieDetailActivity.this,
					// MediaPlayerActivity.class);
					// intent.putExtra(
					// "URL",
					// "http://192.168.1.9:81/mntt/usba1/mov/0/1132__%e9%80%9f%e5%ba%a6%e4%b8%8e%e6%bf%80%e6%83%856/%e9%80%9f%e5%ba%a6%e4%b8%8e%e6%bf%80%e6%83%856.mp4");
					// startActivity(intent);
				}

			}
		});
		return view;
	}

	/**
	 * 获取详情分类视图
	 * 
	 * @return
	 */
	private List<View> getDetailViewList()
	{
		List<View> list = new ArrayList<View>();
		TextView summary = new TextView(this);
		summary.setText(movieInfo.getSummary());
		summary.setTextColor(Color.BLACK);
		list.add(summary);
		// TextView summary1 = new TextView(this);
		// summary1.setText(movieInfo.getSummary());
		// list.add(summary1);
		// TextView summary2 = new TextView(this);
		// summary2.setText(movieInfo.getSummary());
		// list.add(summary2);
		return list;
	}
}
